package tech.horgee.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tech.horgee.common.MyResult;
import tech.horgee.entity.Menu;
import tech.horgee.group.InsertGroup;
import tech.horgee.group.UpdateGroup;
import tech.horgee.service.MenuService;

import java.util.Set;

/**
 * 菜单信息接口
 *
 * @author haojun
 * @since 2022-06-12
 */
@Slf4j
@RestController
@RequestMapping("/menu")
@RequiredArgsConstructor
public class MenuController {
    private final MenuService service;

    /**
     * 新增单条数据
     *
     * @param entity 实体对象
     * @return 新增结果
     */
    @SaCheckPermission("menu:add")
    @PostMapping
    public MyResult save(@RequestBody @Validated(InsertGroup.class) Menu entity) {
        service.save(entity);
        return MyResult.success();
    }

    /**
     * 修改单条数据
     *
     * @param entity 实体对象
     * @return 修改结果
     */
    @SaCheckPermission("menu:edit")
    @PutMapping
    public MyResult update(@RequestBody @Validated(UpdateGroup.class) Menu entity) {
        service.update(entity);
        return MyResult.success();
    }

    /**
     * 批量删除数据
     *
     * @param ids 主键集合
     * @return 操作结果
     */
    @SaCheckPermission("menu:delete")
    @PostMapping("delete")
    public MyResult deleteByIds(@RequestBody Set<Long> ids) {
        service.deleteByIds(ids);
        return MyResult.success();
    }

    /**
     * 获取权限列表和树结构
     */
    @GetMapping("list-tree")
    public MyResult getListAndTree() {
        return MyResult.of(service.getListAndTree());
    }

}
